package com.yaxin.heshanghui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.framework.Utils.PreferencesHelper;
import com.framework.Utils.ScreenUtils;
import com.framework.Utils.ToastUtil;
import com.framework.Utils.Utils;
import com.framework.Utils.Y;
import com.yaxin.heshanghui.R;
import com.yaxin.heshanghui.adapter.BiuooCommonAdapter;
import com.yaxin.heshanghui.adapter.BiuooListItem;
import com.yaxin.heshanghui.adapter.BiuooViewHolder;
import com.yaxin.heshanghui.base.BaseOnClickListener;
import com.yaxin.heshanghui.base.BiuooBaseActivity;
import com.yaxin.heshanghui.customview.CustomFlowLayout;
import com.yaxin.heshanghui.customview.CustomListView;
import com.yaxin.heshanghui.customview.TitleView;
import com.yaxin.heshanghui.utils.BiuooCollectionUtil;
import com.yaxin.heshanghui.utils.BiuooCustomProgressDialogUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


public class BiuooGuideNextActivity extends BiuooBaseActivity implements ActivityCompat.OnRequestPermissionsResultCallback {

    private TitleView titleView;
    private CustomFlowLayout flowLayout;
    private CustomFlowLayout flowLayout2;
    private CustomListView customLv1;
    private BiuooCommonAdapter<BiuooListItem> adapter1;
    private List<BiuooListItem> list1 = new ArrayList<>();
    private boolean _checked;
    private boolean lastChecked;//上个页面是否checked
    private List<BiuooListItem> _list1;
    private List<BiuooListItem> _list2;
    String[] array;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Utils.setTranslucentStatus(this, true);
        Utils.setStatusBarTintDrawable(this, R.mipmap.biuoo_bg_top);
        setContentView(R.layout.biuoo_activity_guide_next);
        array = getResources().getStringArray(R.array.biuoo_phone);
        initView();
        initIntent();
        BiuooCustomProgressDialogUtils.showProcessDialog(this, "正在请求");
        Utils.h.postDelayed(new Runnable() {
            @Override
            public void run() {
                BiuooCustomProgressDialogUtils.dismissProcessDialog();
                initFlowLayout(flowLayout, initData1());
                initFlowLayout(flowLayout2, initData2());
                initList1();
            }
        }, 1000);
    }

    private void initIntent() {
        if (getIntent() != null) {
            _list1 = (List<BiuooListItem>) getIntent().getSerializableExtra("list1");
            _list2 = (List<BiuooListItem>) getIntent().getSerializableExtra("list2");
            if (!BiuooCollectionUtil.getInstance().isEmpty(_list1)) {
                for (int i = 0; i < _list1.size(); i++) {
                    BiuooListItem biuooListItem = _list1.get(i);
                    if (biuooListItem != null && biuooListItem.isChecked()) {
                        lastChecked = true;
                        return;
                    }
                }
            }
            if (!BiuooCollectionUtil.getInstance().isEmpty(_list2)) {
                for (int i = 0; i < _list2.size(); i++) {
                    BiuooListItem biuooListItem = _list2.get(i);
                    if (biuooListItem != null && biuooListItem.isChecked()) {
                        lastChecked = true;
                        return;
                    }
                }
            }
        }
    }

    private boolean checked(String[] pos, int position) {
        if (pos != null) {
            for (int i = 0; i < pos.length; i++) {
                if (TextUtils.equals(pos[i], "" + position)) {
                    return true;
                }
            }
        }
        return false;
    }

    String[] pos;

    private void initList1() {
        String tr = PreferencesHelper.getInstance().getStringData("Position");
        if (!TextUtils.isEmpty(tr)) {
            pos = tr.split(",");
        }
        if (TextUtils.equals(PreferencesHelper.getInstance().getStringData("phone"), array[0])) {
            BiuooListItem item = new BiuooListItem();
            item.setTitle("100元流量套餐");
            item.setPrice("¥100.00" + "元/月");
            item.set_price(100.00);
            item.setShowIcon(false);
            item.setChecked(checked(pos, 0));
            list1.add(item);
            BiuooListItem item2 = new BiuooListItem();
            item2.setTitle("10元10G流量优惠套餐");
            item2.setPrice("¥10.00");
            item2.set_price(10.00);
            item2.setShowIcon(true);
            item2.setChecked(checked(pos, 1));
            list1.add(item2);
            BiuooListItem item3 = new BiuooListItem();
            item3.setTitle("188元4G飞享套餐");
            item3.setPrice("¥188.00" + "元/月");
            item3.set_price(188.00);
            item3.setShowIcon(false);
            item3.setChecked(checked(pos, 2));
            list1.add(item3);
        } else if (TextUtils.equals(PreferencesHelper.getInstance().getStringData("phone"), array[1])) {
            BiuooListItem item = new BiuooListItem();
            item.setTitle("全球通畅聊版");
            item.setPrice("¥88.00" + "元/月");
            item.set_price(88.00);
            item.setShowIcon(false);
            item.setChecked(checked(pos, 0));
            list1.add(item);
            BiuooListItem item2 = new BiuooListItem();
            item2.setTitle("188元4G飞享套餐");
            item2.setPrice("¥188.00" + "元/月");
            item2.set_price(188.00);
            item2.setShowIcon(false);
            item2.setChecked(checked(pos, 1));
            list1.add(item2);
        } else if (TextUtils.equals(PreferencesHelper.getInstance().getStringData("phone"), array[2])) {
            BiuooListItem item = new BiuooListItem();
            item.setTitle("10元10G流量优惠套餐");
            item.setPrice("¥10.00");
            item.set_price(10.00);
            item.setShowIcon(true);
            item.setChecked(checked(pos, 0));
            list1.add(item);
            BiuooListItem item2 = new BiuooListItem();
            item2.setTitle("38元4G飞享套餐青春版");
            item2.setPrice("¥38.00");
            item2.set_price(38.00);
            item2.setShowIcon(false);
            item2.setChecked(checked(pos, 1));
            list1.add(item2);
        } else if (TextUtils.equals(PreferencesHelper.getInstance().getStringData("phone"), array[3])) {
            BiuooListItem item = new BiuooListItem();
            item.setTitle("100元流量套餐");
            item.setPrice("¥100.00" + "元/月");
            item.set_price(100.00);
            item.setShowIcon(false);
            item.setChecked(checked(pos, 0));
            list1.add(item);
            BiuooListItem item2 = new BiuooListItem();
            item2.setTitle("10元10G流量优惠套餐");
            item2.setPrice("¥10.00");
            item2.set_price(10.00);
            item2.setShowIcon(true);
            item2.setChecked(checked(pos, 1));
            list1.add(item2);
            BiuooListItem item3 = new BiuooListItem();
            item3.setTitle("188元4G飞享套餐");
            item3.setPrice("¥188.00" + "元/月");
            item3.set_price(188.00);
            item3.setShowIcon(false);
            item3.setChecked(checked(pos, 2));
            list1.add(item3);
        }
        adapter1 = initAdapter(adapter1);
        customLv1.setAdapter(adapter1);
    }


    private ArrayList<String> initData1() {
        ArrayList<String> list = new ArrayList<>();
        if (TextUtils.equals(PreferencesHelper.getInstance().getStringData("phone"), array[0])) {
            list.add("换机半年");
            list.add("爱好自拍");
            list.add("叠加包爱好者");
        } else if (TextUtils.equals(PreferencesHelper.getInstance().getStringData("phone"), array[1])) {
            list.add("换机一年");
            list.add("商务人士");
        } else if (TextUtils.equals(PreferencesHelper.getInstance().getStringData("phone"), array[2])) {
            list.add("换机半年");
            list.add("网聊达人");
            list.add("性价比控");
        } else if (TextUtils.equals(PreferencesHelper.getInstance().getStringData("phone"), array[3])) {
            list.add("换机一年");
            list.add("果粉");
            list.add("叠加包爱好者");
        }
        return list;
    }

    private ArrayList<String> initData2() {
        ArrayList<String> list = new ArrayList<>();
        if (TextUtils.equals(PreferencesHelper.getInstance().getStringData("phone"), array[0])) {
            list.add("到店5次");
//             list.add("已收藏店铺");
        } else if (TextUtils.equals(PreferencesHelper.getInstance().getStringData("phone"), array[1])) {
            list.add("到店10次");
        } else if (TextUtils.equals(PreferencesHelper.getInstance().getStringData("phone"), array[2])) {
            list.add("到店7次");
        } else if (TextUtils.equals(PreferencesHelper.getInstance().getStringData("phone"), array[3])) {
            list.add("到店15次");
        }
        return list;
    }

    private BiuooCommonAdapter<BiuooListItem> initAdapter(BiuooCommonAdapter<BiuooListItem> adapter) {
        if (adapter == null) {
            adapter = new BiuooCommonAdapter<BiuooListItem>(BiuooGuideNextActivity.this, list1, R.layout.biuoo_listitem_liuliang) {
                @Override
                public View getListItemview(BiuooViewHolder viewHolder, View view, final BiuooListItem item, int position, ViewGroup parent) {
                    viewHolder.setImageResource(R.id.iv_icon, R.mipmap.biuoo_icon_guangdong);
                    viewHolder.setText(R.id.tv_title, item.getTitle());
                    viewHolder.setText(R.id.tv_price, item.getPrice());
                    viewHolder.getView(R.id.iv_upgrade).setVisibility(item.isShowIcon() ? View.GONE : View.GONE);
                    viewHolder.getView(R.id.biuoo_tv_check).setBackgroundResource(!item.isChecked() ? R.mipmap.biuoo_unchoose : R.mipmap.biuoo_choosed);
                    viewHolder.getConvertView().setOnClickListener(new BaseOnClickListener() {
                        @Override
                        protected void onBaseClick(View v) {
                            item.setChecked(!item.isChecked());
                            notifyDataSetChanged();
                            if (item.isChecked()) {
                                _checked = true;
                                return;
                            }

                            if (!BiuooCollectionUtil.getInstance().isEmpty(list1)) {
                                int len = list1.size();
                                for (int i = 0; i < len; i++) {
                                    if (list1.get(i).isChecked()) {
                                        _checked = true;
                                        return;
                                    }
                                }
                            }
                            _checked = false;
                        }
                    });
                    return null;
                }
            };
        }
        return adapter;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        StringBuilder stringBuilder = new StringBuilder();
        if (!BiuooCollectionUtil.getInstance().isEmpty(list1)) {
            int len = list1.size();
            for (int i = 0; i < len; i++) {
                if (list1.get(i).isChecked()) {
                    if (TextUtils.isEmpty(stringBuilder.toString())) {
                        stringBuilder.append(i);
                    } else {
                        stringBuilder.append(",").append(i);
                    }
                }
            }
        }
        if (!TextUtils.isEmpty(stringBuilder.toString())) {
            PreferencesHelper.getInstance().putInfo("Position", stringBuilder.toString());
        }
    }

    private void initView() {
        titleView = (TitleView) findViewById(R.id.titleView);
        titleView.setTitle("导购").setRightTxt("下一步").setRightClickListner(new BaseOnClickListener() {
            @Override
            protected void onBaseClick(View v) {

                if (!_checked && !lastChecked) {
                    Utils.showToast("还未选择任何商品");
                } else {
                    Intent intent = new Intent(BiuooGuideNextActivity.this, BiuooSaleActivity.class);
                    intent.putExtra("list1", (Serializable) _list1);
                    intent.putExtra("list2", (Serializable) _list2);
                    intent.putExtra("list3", (Serializable) list1);
                    startActivity(intent);
                    overridePendingTransition(R.anim.biuoo_slide_right_in,
                            R.anim.biuoo_slide_left_out);
//                startActivity(BiuooSaleActivity.class);
                }
            }
        });
        flowLayout = (CustomFlowLayout) findViewById(R.id.flowLayout);
        flowLayout2 = (CustomFlowLayout) findViewById(R.id.flowLayout2);
        customLv1 = (CustomListView) findViewById(R.id.customLv1);
    }

    private void initFlowLayout(CustomFlowLayout flowLayout, final List<String> list) {
        flowLayout.removeAllViews();
        int length = list.size();
        View[] views = new View[length];
        if (length > 0) {
            int _5dp = ScreenUtils.getInstance().dip2px(this, 5);
            for (int j = 0; j < length; j++) {
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT);
                final ViewGroup viewGroup = null;
                LinearLayout linearLayout = (LinearLayout) LayoutInflater.from(BiuooGuideNextActivity.this).inflate(R.layout.biuoo_inflate_flowlayout,
                        viewGroup, false);
                TextView tv_title = (TextView) linearLayout.findViewById(R.id.tv_title);
                params.setMargins(_5dp, _5dp, _5dp, _5dp);
                tv_title.setLayoutParams(params);
                tv_title.setText(list.get(j));
                views[j] = linearLayout;
            }
            flowLayout.addData(views);
        }
    }
}
